.text
.align 8
.extern kernel64_main
.extern vsprintf
.extern strlen
.extern console_write
.extern printk_buff

.globl _start
.type _start, @function
_start:
    xchg %bx, %bx
    xchg %bx, %bx
.init_8259a:
    // 向主发送ICW1
    movb $0x11, %al
    outb %al, $0x20

    // 向从发送ICW1
    outb %al, $0xa0

    // 向主发送ICW2
    movb $0x20, %al
    outb %al, $0x21

    // 向从发送ICW2
    movb $0x28, %al
    outb %al, $0xa1

    // 向主从发送ICW3
    movb $0x4, %al
    outb %al, $0x21

    movb $0x02, %al
    outb %al, $0xA1

    // 向主从发送ICW4
    movb $0x03, %al
    outb %al, $0x21

    outb %al, $0xA1

.enable_8259a_main:
    movb $0b11111110, %al 
    outb %al, $0x21

.disable_8259a_slave:
    movb $0b11111101, %al 
    outb %al, $0xA1

.enable_rtc:
    movb $0x0b, %al
    orb $0x80, %al
    outb %al, $0x70

    movb $0x52, %al
    outb %al, $0x71

    movb $0x8a, %al
    outb %al, $0x70

    movb $0x2f, %al
    outb %al, $0x71

    movb $0x0c, %al
    outb %al, $0x70
    inb $0x71, %al

    inb $0xa1, %al
    andb $0xfe, %al
    outb %al, $0xa1

.set_8253_frequency:
    movb $0x36, %al
    outb %al, $0x43

    movw $11932, %ax
    outb %al, $0x40
    movb %ah, %al
    outb %al, $0x40

.end_kernel64_main:
    call kernel64_main
    jmp .

.globl printk
.type printk, @function
printk:
    pushq %rdi
    pushq %r9
    pushq %r8
    pushq %rcx
    pushq %rdx
    pushq %rsi

    movq %rdi, %rsi
    movq printk_buff, %rdi
    movq %rsp, %rdx
    call vsprintf

    movq printk_buff, %rdi
    call strlen

    movq printk_buff, %rdi
    movq %rax, %rsi
    call console_write

    addq $(8 * 6), %rsp
    ret